package com.google.gsaguide.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.google.gsaguide.R;
import com.google.gsaguide.gsa.Ambassador;
import com.google.gsaguide.volley.AppController;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

/**
 * Created by Felipe on 26/06/2014.
 */
public class AmbassadorsListAdapter extends BaseAdapter {
    // TODO: Need to optimize and comment this code.
    private List<Ambassador> mItems;
    private LayoutInflater mInflater;
    private ImageLoader mImageLoader;

    public AmbassadorsListAdapter(LayoutInflater inflater, Context context) {
        mInflater = inflater;
        mItems = new ArrayList<Ambassador>();
        mImageLoader = AppController.getInstance().getImageLoader(context);
    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public Ambassador getItem(int position) {
        return mItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.ambassador_list_item, parent, false);

            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.ambassadorName);
            holder.info = (TextView) convertView.findViewById(R.id.ambassadorInfo);
            holder.photo = (NetworkImageView) convertView.findViewById(R.id.ambassadorPhoto);

            holder.photo.setDefaultImageResId(R.drawable.unknown_user);
            holder.photo.setErrorImageResId(R.drawable.unknown_user);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Ambassador ambassador = mItems.get(position);

        holder.name.setText(ambassador.getName());
        holder.info.setText(ambassador.getUniversity() + " - " + ambassador.getLocation());
        holder.photo.setImageUrl(ambassador.getPhotoURL(), mImageLoader);

        return convertView;
    }

    public void addItems(List<Ambassador> items) {
        if (mItems == null) {
            mItems = new ArrayList<Ambassador>();
        }
        mItems.addAll(items);
        sortList();
        notifyDataSetChanged();
    }

    public void addItem(Ambassador item) {
        if (mItems == null) {
            mItems = new ArrayList<Ambassador>();
        }
        mItems.add(item);
        sortList();
        notifyDataSetChanged();
    }

    public void sortList() {
        Collections.sort(
                mItems,
                new Comparator<Ambassador>() {
                    Collator collator = Collator.getInstance(new Locale("pt", "BR"));

                    public int compare(Ambassador lhs, Ambassador rhs) {
                        return collator.compare(lhs.getName(), (rhs.getName()));
                    }
                }
        );
    }

    private class ViewHolder {
        public TextView name;
        public TextView info;
        public NetworkImageView photo;
    }
}